home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / BasicArrowButton.java < prev    next >
Text File  |  1998-06-30  |  7KB  |  210 lines

  1. /*
  2.  * @(#)BasicArrowButton.java    1.11 98/02/02
  3.  *
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  *
  19.  */
  20. package com.sun.java.swing.plaf.basic;
  21.  
  22. import java.awt.Dimension;
  23. import java.awt.Graphics;
  24. import java.awt.Color;
  25.  
  26. import com.sun.java.swing.*;
  27.  
  28. /**
  29.  * JButton object that draws a scaled Arrow in one of the cardinal directions.
  30.  * <p>
  31.  * Warning: serialized objects of this class will not be compatible with
  32.  * future swing releases.  The current serialization support is appropriate 
  33.  * for short term storage or RMI between Swing1.0 applications.  It will
  34.  * not be possible to load serialized Swing1.0 objects with future releases
  35.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  36.  * baseline for the serialized form of Swing objects.
  37.  *
  38.  * @version 1.11 02/02/98
  39.  * @author David Kloba
  40.  */
  41. public class BasicArrowButton extends JButton implements SwingConstants
  42. {
  43.         protected int direction;
  44.  
  45.         public BasicArrowButton(int direction)            {
  46.             setDirection(direction);
  47.             setBackground(UIManager.getColor("control"));
  48.         }
  49.  
  50.         public int getDirection() { return direction; }
  51.  
  52.         public void setDirection(int dir) { direction = dir; }
  53.  
  54.     public void paint(Graphics g) {
  55.         Color origColor;
  56.         boolean isPressed, isEnabled;
  57.         int w, h, size;
  58.  
  59.             w = getSize().width;
  60.             h = getSize().height;
  61.         origColor = g.getColor();
  62.         isPressed = getModel().isPressed();
  63.         isEnabled = isEnabled();
  64.  
  65.             g.setColor(getBackground());
  66.             g.fillRect(1, 1, w-2, h-2);
  67.  
  68.             /// Draw the proper Border
  69.             if (isPressed) {
  70.                 g.setColor(UIManager.getColor("controlShadow"));
  71.                 g.drawRect(0, 0, w-1, h-1);
  72.             } else {
  73.                 // Using the background color set above
  74.                 g.drawLine(0, 0, 0, h-1);
  75.                 g.drawLine(1, 0, w-2, 0);
  76.  
  77.                 g.setColor(UIManager.getColor("controlHighlight"));    // inner 3D border
  78.                 g.drawLine(1, 1, 1, h-3);
  79.                 g.drawLine(2, 1, w-3, 1);
  80.  
  81.                 g.setColor(UIManager.getColor("controlShadow"));       // inner 3D border
  82.                 g.drawLine(1, h-2, w-2, h-2);
  83.                 g.drawLine(w-2, 1, w-2, h-3);
  84.  
  85.                 g.setColor(UIManager.getColor("controlDkShadow"));     // black drop shadow  __|
  86.                 g.drawLine(0, h-1, w-1, h-1);
  87.                 g.drawLine(w-1, h-1, w-1, 0);
  88.             }
  89.  
  90.             // If there's no room to draw arrow, bail
  91.             if(h < 5 || w < 5)      {
  92.                 g.setColor(origColor);
  93.                 return;
  94.             }
  95.  
  96.             if (isPressed) {
  97.                 g.translate(1, 1);
  98.             }
  99.  
  100.             // Draw the arrow
  101.             size = Math.min((h - 4) / 3, (w - 4) / 3);
  102.             size = Math.max(size, 2);
  103.         paintTriangle(g, (w - size) / 2, (h - size) / 2,
  104.                 size, direction, isEnabled);
  105.  
  106.             // Reset the Graphics back to it's original settings
  107.             if (isPressed) {
  108.                 g.translate(-1, -1);
  109.         }
  110.         g.setColor(origColor);
  111.  
  112.         }
  113.  
  114.         public Dimension getPreferredSize() {
  115.             return new Dimension(16, 16);
  116.         }
  117.  
  118.         public Dimension getMinimumSize() {
  119.             return new Dimension(5, 5);
  120.         }
  121.  
  122.         public Dimension getMaximumSize() {
  123.             return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
  124.         }
  125.     
  126.         public boolean isFocusTraversable() {
  127.       return false;
  128.     }
  129.  
  130.     public void requestFocus() {
  131.     }
  132.     
  133.     public void paintTriangle(Graphics g, int x, int y, int size, 
  134.                     int direction, boolean isEnabled) {
  135.         Color oldColor = g.getColor();
  136.         int mid, i, j;
  137.  
  138.         j = 0;
  139.             size = Math.max(size, 2);
  140.         mid = size / 2;
  141.     
  142.         g.translate(x, y);
  143.         if(isEnabled)
  144.         g.setColor(UIManager.getColor("controlDkShadow"));
  145.         else
  146.         g.setColor(UIManager.getColor("controlShadow"));
  147.  
  148.             switch(direction)       {
  149.             case NORTH:
  150.                 for(i = 0; i < size; i++)      {
  151.                     g.drawLine(mid-i, i, mid+i, i);
  152.                 }
  153.                 if(!isEnabled)  {
  154.                     g.setColor(UIManager.getColor("controlHighlight"));
  155.                     g.drawLine(mid-i+2, i, mid+i, i);
  156.                 }
  157.                 break;
  158.             case SOUTH:
  159.                 if(!isEnabled)  {
  160.                     g.translate(1, 1);
  161.                     g.setColor(UIManager.getColor("controlHighlight"));
  162.                     for(i = size-1; i >= 0; i--)   {
  163.                         g.drawLine(mid-i, j, mid+i, j);
  164.                         j++;
  165.                     }
  166.             g.translate(-1, -1);
  167.             g.setColor(UIManager.getColor("controlShadow"));
  168.         }
  169.         
  170.         j = 0;
  171.                 for(i = size-1; i >= 0; i--)   {
  172.                     g.drawLine(mid-i, j, mid+i, j);
  173.                     j++;
  174.                 }
  175.                 break;
  176.             case WEST:
  177.                 for(i = 0; i < size; i++)      {
  178.                     g.drawLine(i, mid-i, i, mid+i);
  179.                 }
  180.                 if(!isEnabled)  {
  181.                     g.setColor(UIManager.getColor("controlHighlight"));
  182.                     g.drawLine(i, mid-i+2, i, mid+i);
  183.                 }
  184.                 break;
  185.             case EAST:
  186.                 if(!isEnabled)  {
  187.                     g.translate(1, 1);
  188.                     g.setColor(UIManager.getColor("controlHighlight"));
  189.                     for(i = size-1; i >= 0; i--)   {
  190.                         g.drawLine(j, mid-i, j, mid+i);
  191.                         j++;
  192.                     }
  193.             g.translate(-1, -1);
  194.             g.setColor(UIManager.getColor("controlShadow"));
  195.                 }
  196.  
  197.         j = 0;
  198.                 for(i = size-1; i >= 0; i--)   {
  199.                     g.drawLine(j, mid-i, j, mid+i);
  200.                     j++;
  201.                 }
  202.         break;
  203.             }
  204.         g.translate(-x, -y);    
  205.         g.setColor(oldColor);
  206.     }
  207.     
  208. }
  209.  
  210.